package com.wzy.serviceLocatorPattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: wzy
 * @Date: 2023/01/31/19:17
 * @Description:
 */
@Service
public class Client {
    private ParserFactory parserFactory;

    @Autowired
    public Client(ParserFactory parserFactory) {
        this.parserFactory = parserFactory;
    }

    public List getAll(ContentType contentType) {
        // 关键点，直接根据类型获取
        return parserFactory
                .getParser(contentType)
                .parse();
    }

    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();
        context.refresh();
        ParserFactory parserFactory = (ParserFactory) context.getBean("parserFactory", ParserFactory.class);
        context.registerBean("client", Client.class, () -> {
            Client client = new Client(parserFactory);
            return client;
        });
        Client client = (Client) context.getBean("client", Client.class);
        client.getAll(ContentType.CSV);
    }
}
